import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_nb_net/flutter_net.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:icons_plus/icons_plus.dart';
import 'package:tracker/common/enums/user_type_enum.dart';
import 'package:tracker/common/log/x_logger.dart';
import 'package:tracker/pages/home/home/model/home_device_info_resp.dart';
import 'package:tracker/pages/home/home/model/user_info_resp.dart';
import 'package:tracker/provider/global_provider.dart';
import 'package:tracker/router/router_manager.dart';

/// 菜单
class ProfileMenu {
  late String title;
  late String code;
  late Icon icon;

  ProfileMenu(this.title, this.code, this.icon);
}

mixin ProfilePageMixin<T extends ConsumerStatefulWidget> on ConsumerState<T> {
  /// 个人中心菜单
  final menuList = [
    ProfileMenu('添加设备', 'bindDevice', const Icon(IonIcons.add_circle)),
    ProfileMenu('解绑设备', 'unbindDevice', const Icon(IonIcons.remove_circle)),
    ProfileMenu('修改密码', 'changePassword', const Icon(IonIcons.shield)),
    ProfileMenu('消息设置', 'notificationSetting', const Icon(IonIcons.notifications)),
    // ProfileMenu('在线客服', 'customerService', const Icon(IonIcons.person)),
    ProfileMenu('用户服务协议', 'userServiceAgreement', const Icon(IonIcons.newspaper)),
    ProfileMenu('隐私政策', 'privacyPolicy', const Icon(IonIcons.warning)),
    // ProfileMenu('检查更新', 'checkUpdate', const Icon(IonIcons.arrow_down_circle)),
    ProfileMenu('关于', 'about', const Icon(IonIcons.information_circle)),
    ProfileMenu('退出登录', 'logout', const Icon(IonIcons.log_out)),
  ];

  /// 用户设备数量
  int userDeviceCount = 0;

  /// 根据用户类型过滤菜单
  void filterMenuList(UserTypeEnum userType) {
    if (userType == UserTypeEnum.agent) {
      // 代理商
      menuList.removeWhere((element) => element.code == 'unbindDevice');
      menuList.removeWhere((element) => element.code == 'bindDevice');
      menuList.removeWhere((element) => element.code == 'notificationSetting');
    } else if (userType == UserTypeEnum.phone) {
      // 手机号登录
    } else if (userType == UserTypeEnum.device) {
      // 设备登录
      menuList.removeWhere((element) => element.code == 'unbindDevice');
    }
  }

  /// 根据用户类型获取登录类型名称
  String getLoginTypeName(UserTypeEnum userType) {
    if (userType == UserTypeEnum.agent) {
      return '用户名登录';
    } else if (userType == UserTypeEnum.phone) {
      return '手机号登录';
    } else if (userType == UserTypeEnum.device) {
      return '设备号登录';
    }
    return '';
  }

  /// 根据用户类型获取登录名
  String getLoginName(UserTypeEnum userType, UserInfoData userInfo, DeviceInfoVO deviceInfo) {
    if (userType == UserTypeEnum.phone) {
      return userInfo.phone ?? '';
    } else if (userType == UserTypeEnum.device) {
      return deviceInfo.imei ?? '';
    }
    return userInfo.nickname ?? '';
  }

  /// 退出登录
  void handleLogout() async {
    try {
      await post('/app/user/logout');
    } catch (e) {
      XLogger.error('退出登录失败：$e');
    }
    // 清除用户信息
    SpUtil.putBool('isLogin', false);
    ref.read(deviceInfoProvider.notifier).removeDeviceInfo();
    // 清除设备信息
    ref.read(userInfoProvider.notifier).removeUserInfo();
    // 跳转到登录页
    context.go(RouterManager.login);
  }

  /// 查询用户设备数量
  Future queryUserDeviceCount() async {
    final userInfo = ref.watch(userInfoProvider);
    userDeviceCount = userInfo.deviceCount ?? 0;
    if (userDeviceCount == 0) {
      // 刷新用户信息
      await ref.read(userInfoProvider.notifier).fetchUserInfo((userInfoData) {
        userDeviceCount = userInfoData.deviceCount ?? 0;
      });
    }
  }
}
